package View;

import java.util.ArrayList;
import java.util.Scanner;

import Controller.*;
import Helper.Helper;
import Model.*;

public class Index {
    public static void index(){
        Helper.clear();
        System.out.println("*-----[ 欢迎进入电子商城 ]-----*\n");

        System.out.println("\t 1. 登录");
        System.out.println("\t 2. 注册\n");

        System.out.println("\t 3. 浏览商品");
        System.out.println("\t 4. 搜索商品\n");

        System.out.println("\t 5. 管理员登录\n");

        System.out.println("\t 6. 退出");

        System.out.println("*---------------------------*");
    }

    public static boolean login(){
        System.out.println("[ 用户登录 ]\n");
        System.out.print("用户名: ");

        Scanner scanner = new Scanner(System.in);
        String username = scanner.next();
        while( !username.matches("[0-9a-zA-Z_]+") ){
            System.err.println("用户名只允许由字母数字下划线组成！");
            System.out.print("请重新输入用户名： ");

            username = scanner.next();
        }

        String password = Helper.getPassword("密码: ");

        return UserController.login(username, password);
    }

    public static boolean signup(){
        System.out.println("[ 用户注册 ]");
        System.out.print("用户名: ");

        Scanner scanner = new Scanner(System.in);
        String username = scanner.next();
        while( username.length()>50 || !username.matches("[0-9a-zA-Z_]+") ){
            System.err.println("用户名只能由字母数字下划线组成，且小于50字节！");
            System.out.print("请重新输入用户名： ");

            username = scanner.next();
        }

        String password = Helper.getPassword("密码: ");

        while( !Helper.getPassword("确认密码: ").equals(password) ){
            System.err.println("两次密码不一致，需重新设置密码！");

            password = Helper.getPassword("密码: ");
        }

        if( !Helper.brancher("确定？", true) ){
            return false;
        }

        boolean status = UserController.signup(username, password);

        if( status ){
            System.out.println("注册成功！");
        }else{
            System.err.println("注册失败！");
        }

        Helper.waitter();

        return status;
    }

    public static void listProducts(){
        System.out.println("[ 所有商品 ]\n");

        System.out.println("编号 |   名称   |   价格   | 数量");
        ArrayList<Product> products = ProductController.listProducts();

        for( Product item : products ){
            if( item.amount > 0 ){
                System.out.printf("%4d  %6s  %2.1f  %4d\n", item.id ,item.name, item.price, item.amount);
            }
        }

        Helper.waitter();
    }

    public static void searchProduct(){
        System.out.println("[ 搜索商品 ]\n");
        System.out.print("请输入商品名: ");

        Scanner scanner = new Scanner(System.in);
        String productName = scanner.next();
        while( productName.matches("[`~!@#$%^&*()+=|{}':;,\\[\\].<>/?！￥…（）—]+") ){
            System.err.println("商品名不合法！");
            System.out.print("请重新输入商品名： ");

            productName = scanner.next();
        }

        System.out.println("编号 |   名称   |   价格  | 数量");
        ArrayList<Product> products = ProductController.searchProduct(productName);

        for( Product item : products ){
            if( item.amount > 0 ){
                System.out.printf("%4d  %6s  %2.1f  %4d\n", item.id ,item.name, item.price, item.amount);
            }
        }

        Helper.waitter();
    }

    public static boolean adminLogin(){
        System.out.println("[ 管理员登录 ]\n");
        System.out.print("用户名: ");

        Scanner scanner = new Scanner(System.in);
        String username = scanner.next();
        while( !username.matches("[0-9a-zA-Z_]+") ){
            System.err.println("用户名只允许由字母数字下划线组成！");
            System.out.print("请重新输入用户名： ");

            username = scanner.next();
        }

        String password = Helper.getPassword("密码: ");

        return AdminController.login(username, password);
    }
}
